package com.company.project.modular.standard.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.project.core.base.controller.BaseController;
import com.company.project.core.base.tips.ErrorTip;
import com.company.project.core.log.LogObjectHolder;
import com.company.project.modular.system.model.Instock;
import com.company.project.modular.system.model.Standard;
import com.company.project.modular.system.service.IInstockService;
import com.company.project.modular.system.service.IStandardService;
import com.company.project.modular.system.service.IUseService;
import com.company.project.modular.system.model.Use;
import com.company.project.modular.system.warpper.UseWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对照品使用登记控制器
 *
 * @author fengshuonan
 * @Date 2018-10-30 15:41:26
 */
@Controller
@RequestMapping("/use")
public class UseController extends BaseController {

    private String PREFIX = "/standard/use/";

    @Reference
    private IUseService useService;
    @Reference
    private IStandardService standardService;
    @Reference
    private IInstockService instockService;

    /**
     * 跳转到对照品使用登记首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "use.html";
    }

    /**
     * 跳转到添加对照品使用登记
     */
    @RequestMapping("/use_add")
    public String useAdd() {

        return PREFIX + "use_add.html";
    }
    /**
     * 跳转到读取添加对照品使用登记
     */
    @RequestMapping(value = "/useRead_add/{useCode}")
    @ResponseBody
    public Map<String,Object> useRead(@PathVariable String useCode,Model model) {
        List<Instock> i = instockService.selectByAll(useCode);/*根据标识码去查询标准表和库存表*/

        List<Standard> s = standardService.selectByAll(useCode);/*根据标识码去查询标准表和库存表*/
        Map<String,Object> jsonObject = new HashMap<String, Object>();  //创建Json对象
        //设置Json对象的属性
        jsonObject.put("useName", s.get(0).getStandardName());
        jsonObject.put("useLabel", s.get(0).getStandardLabel());
        jsonObject.put("useId", s.get(0).getStandardId());
        jsonObject.put("usePath", s.get(0).getStandardPath());
        if(i.size() !=0 && s.size() != 0){
            jsonObject.put("useNumber", i.get(0).getStandardNumber());
        }else{
            jsonObject.put("useNumber", 0);
        }
        return jsonObject;
    }

    /**
     * 跳转到修改对照品使用登记
     */
    @RequestMapping("/use_update/{useId}")
    public String useUpdate(@PathVariable Integer useId, Model model) {
        Use use = useService.selectById(useId);
        model.addAttribute("item",use);
        LogObjectHolder.me().set(use);
        return PREFIX + "use_edit.html";
    }

    /**
     * 获取对照品使用登记列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> use = useService.selectMaps(null);
        return new UseWarpper(use).warp();
    }

    /**
     * 获取标识码standardCode
     */
    @RequestMapping("/useCode")
    @ResponseBody
    public List<String> standardCode2(Model model) {
        /*查询标准物质表里面的标识码*/
        List<String> useCode = standardService.findByStandarCode();
        return useCode;
    }
    /**
     * 新增对照品使用登记
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Use use, Instock instock,BindingResult bindingResult) {
       /* 剩余量的计算*/
        Double useAccess = use.getUseAccess();//取用量
        Double useNumber = use.getUseNumber();//获取页面上的剩余量
        if(useNumber-useAccess > 0  || useNumber-useAccess == 0){
            /*添加一条出库记录*/
            long l = System.currentTimeMillis();  //得到long类型当前时间  //new日期对象
            Date date = new Date(l);
            /*SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");*///转换提日期输出格式
            instock.setStandardOutDate(date);//出库日期
            instock.setStandardOutNumber(useAccess);//出库数量
            List<Instock> i = instockService.selectByAll(use.getUseCode());/*根据标识码去查询标准表和库存表*/
            if(i.size() != 0){
                int standardFrequency = i.get(0).getStandardFrequency();
                instock.setStandardFrequency(standardFrequency + 1); //总次数+1
                int standardOutFrequency = i.get(0).getStandardOutFrequency();
                instock.setStandardOutFrequency(standardOutFrequency + 1);//出库次数+1
            }else{
                instock.setStandardFrequency(1); //总次数为1
                instock.setStandardOutFrequency(1);//出库次数为1
            }
            instock.setStandardNumber(useNumber-useAccess);//剩余量
            instock.setStandardCode(use.getUseCode());//出库标识码
            instockService.insert(instock);
            use.setUseNumber(useNumber-useAccess);
            useService.insert(use);
            return SUCCESS_TIP;
        }else{
            return new ErrorTip(0, "取用量超过库存量");
        }
    }

    /**
     * 删除对照品使用登记
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer useId) {
        useService.deleteById(useId);
        return SUCCESS_TIP;
    }

    /**
     * 修改对照品使用登记
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Use use,BindingResult bindingResult) {
        useService.updateById(use);
        return SUCCESS_TIP;
    }

    /**
     * 对照品使用登记详情
     */
    @RequestMapping(value = "/detail/{useId}")
    @ResponseBody
    public Object detail(@PathVariable("useId") Integer useId) {
        return useService.selectById(useId);
    }
}
